package com.windy.dailynews.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.windy.dailynews.R;
import com.windy.dailynews.model.Summary;

import java.util.List;

/**
 * Created by dingfeng on 2016/11/22.
 */
public class NewsListAdapter extends BaseListAdapter<Summary> {

    public NewsListAdapter(Context context, List<Summary> list) {
        super(context, list);
    }

    @Override
    public View bindView(int position, View convertView, ViewGroup parent) {
        Summary summary = mList.get(position);
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_summary, parent, false);
            holder = new ViewHolder();
            holder.iv_summary = (ImageView) convertView.findViewById(R.id.iv_summary);
            holder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tv_title.setText(summary.getTitle());

        if (summary.getImages() != null && summary.getImages().get(0) != null) {
            holder.iv_summary.setVisibility(View.VISIBLE);
            Glide.with(mContext.getApplicationContext())
                    .load(summary.getImages().get(0))
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(holder.iv_summary);
        } else {
            holder.iv_summary.setVisibility(View.GONE);
        }

        return convertView;
    }

    class ViewHolder {
        ImageView iv_summary;
        TextView tv_title;
    }

}


